﻿#region copyright
//Copyright 2013 Ramon Ordiales Plaza
//
//This file is part of TrayFastMsg.
//
//    TrayFastMsg is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    TrayFastMsg is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with FastMessage.  If not, see <http://www.gnu.org/licenses/>.
//
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ROPControls
{
    public static class HelperIcon
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        extern static bool DestroyIcon(IntPtr handle);

        public static Icon IconFromBmp(Image img)
        {
            using (Bitmap bmp = new Bitmap(img, new Size(64, 64)))
            {
                IntPtr Hicon = bmp.GetHicon();
                Icon newIcon = Icon.FromHandle(Hicon);
                using (System.IO.MemoryStream fs = new System.IO.MemoryStream())
                {
                    newIcon.Save(fs);
                    fs.Position = 0;
                    DestroyIcon(Hicon);
                    return new Icon(fs);
                }
            }
        }
        private static byte[] pngiconheader = new byte[]{0,0,1,0,1,0,0,0,0,0,1,0,24,0,0,0,0,0,0,0,0,0};
        public static Icon PngIconFromImage(Image img,int size=16)
        {
            using (Bitmap bmp = new Bitmap(img, new Size(size, size)))
            {
                byte[] png;
                using (System.IO.MemoryStream fs = new System.IO.MemoryStream())
                {
                    bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                    fs.Position = 0;
                    png=fs.ToArray();
                }
                
                using (System.IO.MemoryStream fs = new System.IO.MemoryStream())
                {
                    if (size >= 256) size = 0;
                    pngiconheader[6] = (byte)size;
                    pngiconheader[7] = (byte)size;
                    pngiconheader[14]=(byte)(png.Length & 255);
                    pngiconheader[15] = (byte)(png.Length / 256);
                    pngiconheader[18] = (byte)(pngiconheader.Length);

                    fs.Write(pngiconheader, 0, pngiconheader.Length);
                    fs.Write(png, 0, png.Length);
                    fs.Position = 0;
                    return new Icon(fs);
                }
            }
        }

    }
}
